package com.ssyc.gsk_parents.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.OnloadingMoreListener;
import com.ssyc.common.manager.PickerManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.DateUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UIHelper;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.adapter.HomeWorkRvAdapter;
import com.ssyc.gsk_parents.bean.HomeWorkInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 宝贝练习
 * Created by Administrator on 2018/6/20 0020.
 */

public class ParentsBabyHomeWorkActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener, OnloadingMoreListener {

    //返回键
    private ImageView ivBack;

    //加载框
    private RelativeLayout rlLoading;

    //选择日期的图片
    private ImageView ivChooseDate;

    //列表控件
    private RecyclerView rv;

    //下拉刷新控件
    private SmartRefreshLayout srf;

    //定义上传服务器的时间,格式暂定为yyyy年-MM月
    private String time;

    //列表数据
    private List<HomeWorkInfo.HomeworkBean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private HomeWorkRvAdapter adapter;

    //分页
    private int page = 0;


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_baby_homework;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新
        initSrf();

        //设置查询的初始化时间
        initDate();

        //设置列表
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 初始化下拉刷新控件
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 初始化视图的方法
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);

        ivChooseDate = findViewById(R.id.iv_choose_date);
        ivChooseDate.setOnClickListener(this);

        rlLoading = findViewById(R.id.rl_loading_view);
        rlLoading.setVisibility(View.VISIBLE);
    }

    /**
     * 设置初始化查询时间
     */
    private void initDate() {
        time = DateUtils.getTime(new Date(), "yyyy-MM");
    }


    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMsg = emptyView.findViewById(R.id.tv_content);
        tvMsg.setText("您的宝贝暂时没有收到练习,请联系老师");
        adapter = new HomeWorkRvAdapter(this, R.layout.parent_rv_homework, oldDatas);
    }

    /**
     * 请求接口数据
     *
     * @param method
     */
    private void http(int method) {
        if (method == BqaManager.NORML || method == BqaManager.PULLREFRESH) {
            page = 0;
        } else {
            page++;
        }
        Map<String, String> map = new HashMap<>();
        map.put("type", "13");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("time", time);
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getToken(this));

        HttpUtils.post(HttpAdress.HOMEWORKLIST, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoading) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (null != rlLoading && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    HomeWorkInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, HomeWorkInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            List<HomeWorkInfo.HomeworkBean> newDatas = info.getHomework();
                            Log.i("test", "新数据是：" + newDatas.size());
                            if (method == BqaManager.NORML) {
                                oldDatas.clear();
                                oldDatas.addAll(newDatas);
                                BqaManager.setRv(emptyView, ParentsBabyHomeWorkActivity.this, adapter, rv);
                            } else {
                                BqaManager.updateRvBySmf(BqaManager.PULLREFRESH, srf, rv, oldDatas, newDatas, adapter);
                                if (method == BqaManager.PULLREFRESH) {
                                    adapter.setEnableLoadMore(false);
                                }
                            }
                        } else if ("104".equals(info.getState())) {
                            UIHelper.showToast(ParentsBabyHomeWorkActivity.this, "暂无数据");
                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_choose_date) {
            //选择日期
            showDatePick();
        }
    }

    /**
     * 选择日期的方法
     */
    private void showDatePick() {
        PickerManager.showPickerByYeaerAndMonth(this, new PickerManager.onSelectedListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                //选中后的回调
                time = DateUtils.getTime(date, "yyyy-MM");
                Log.i("test", "time=" + time);
                http(BqaManager.NORML);
            }
        });
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMore() {
//        http(BqaManager.LOADINDGMORE);
    }
}
